package cn.vcjf.weapp.api.payment.service;

import cn.vcjf.weapp.api.goods.entity.GoodsOrder;
import cn.vcjf.weapp.api.goods.entity.GoodsOrderItem;
import cn.vcjf.weapp.api.goods.entity.GoodsRefundOrder;
import cn.vcjf.weapp.api.goods.mapper.GoodsOrderMapper;
import cn.vcjf.weapp.api.goods.mapper.GoodsRefundOrderMapper;
import cn.vcjf.weapp.api.goods.service.GoodsGroupService;
import cn.vcjf.weapp.api.payment.qfwxpay.service.AbstractQfCallBackService;
import cn.vcjf.weapp.api.user.entity.UserProfit;
import cn.vcjf.weapp.api.user.service.UserProfitService;
import com.alibaba.fastjson.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @Auther: zhengzihang
 * @Date: 2018/10/16 17:12
 * @Description:
 */
@Service
public class QfCallBackServiceImpl extends AbstractQfCallBackService<GoodsOrder, GoodsRefundOrder> {

	private Logger logger = LogManager.getLogger(QfCallBackServiceImpl.class);

	@Resource
	private GoodsOrderMapper goodsOrderMapper;
	@Resource
	private GoodsRefundOrderMapper goodsRefundOrderMapper;
	@Resource
	private GoodsGroupService goodsGroupService;
	@Resource
	private UserProfitService userProfitService;


	//		(1,待审核,2,已审核,3,拒绝,4申请成功,5已完成,6失败)
	@Override
	protected int getRefOrderFailedStatus() {
		return 6;
	}

	@Override
	protected int getRefOrderOkStatus() {
		return 5;
	}

	@Override
	protected int getRefOrderStatus(GoodsRefundOrder order) {
		return order.getStatus();
	}

	@Override
	protected void updateRefOrderRefundOk(GoodsRefundOrder order, String orderDes, String allRes) {
		order.setStatus(getRefOrderOkStatus());
		order.setOrderDes(orderDes);
		order.setCallBackRes(allRes);
		goodsRefundOrderMapper.updateById(order);
	}

	@Override
	protected void updateRefOrderRefundFailed(GoodsRefundOrder order, String orderDes, String allRes) {
		order.setStatus(getRefOrderFailedStatus());
		order.setOrderDes(orderDes);
		order.setCallBackRes(allRes);
		goodsRefundOrderMapper.updateById(order);
	}

	@Override
	protected GoodsRefundOrder findRefOrderByOrderNo(String orderNo) {
		GoodsRefundOrder query = new GoodsRefundOrder();
		query.setOrderNo(orderNo);
		return goodsRefundOrderMapper.selectOne(query);
	}

	@Override
	protected void doAfterCallback(boolean orderPayFlag,GoodsOrder order,GoodsRefundOrder rOrder) {
		if(orderPayFlag) {
			//支付订单
			if(order!=null) {
				//减少库存
				Integer collageOrderId = order.getCollageOrderId();
				//活动订单,购买数量是1
				if(collageOrderId!=null){
					goodsGroupService.reduceGoodsGroupRepertory(order.getGoodsId(), 1);
				}else {
					List<GoodsOrderItem> orderItems = order.getOrderItems();
					for (GoodsOrderItem item : orderItems) {
						goodsGroupService.reduceGoodsGroupRepertory(item.getGoodsId(), item.getGoodsNum());
					}
				}
				//订单支付成功,分润计算
				try {

					UserProfit userProfit = new UserProfit();
					userProfit.setOrderNo(order.getOrderNo());
					userProfit.setUserId(order.getUserId());
					userProfit.setOrderMoney(order.getTraActualMoney());

					userProfitService.calculateUserProfit(userProfit);
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("订单分润计算失败:{}", JSONObject.toJSONString(order));
				}

				//退款订单
			}else if(rOrder!=null){
				rOrder.getGoodsId();
				goodsGroupService.addGoodsGroupRepertory(rOrder.getGoodsId(),1);
			}
		}
	}

	/**
	 * 获取交易订单
	 * @param orderNo
	 * @return
	 */
	@Override
	protected GoodsOrder findOrderByOrderNo(String orderNo) {
		return null;
	}
	//0,创建中.1,下单成功,待支付.2,已付款,待发货.3已发货.4已完成.5下单失败.6支付失败.7退款
	@Override
	protected int getOrderStatus(GoodsOrder order) {
		return order.getStatus();
	}

	@Override
	protected int getOrderOkStatus() {
		return 2;
	}

	@Override
	protected List<Integer> getOrderFinishStatus() {

		return Arrays.asList(new Integer[]{2,3,4,5});
	}

	@Override
	protected int getOrderFailedStatus() {
		return 6;
	}

	@Override
	protected int updateOrderSuccess(GoodsOrder order, String orderDes, String allRes) {
		order.setStatus(GoodsOrder.ORDER_STATUS_PAYED);
		order.setOrderDes(orderDes);
		order.setpResult(allRes);
		return goodsOrderMapper.updateById(order);
	}

	@Override
	protected int updateOrderFailed(GoodsOrder order, String orderDes, String allRes) {
		order.setStatus(GoodsOrder.ORDER_STATUS_PAY_FAILED);
		order.setOrderDes(orderDes);
		order.setpResult(allRes);
		return goodsOrderMapper.updateById(order);
	}

	@Override
	protected int updateOrderRefund(GoodsOrder order, String orderDes, String allRes) {
		order.setStatus(GoodsOrder.ORDER_STATUS_REFUNDED);
		order.setOrderDes(orderDes);
		order.setpResult(allRes);
		return goodsOrderMapper.updateById(order);
	}
}
